//
//  FFPasswordFreePayVC.m
//  LLWallet
//
//  Created by tt on 2020/10/14.
//

#import "FFPasswordFreePayVC.h"
#import "FFWalletManager.h"
@interface FFPasswordFreePayVC ()<FFPasswordFreePayVMDelegate>

@end

@implementation FFPasswordFreePayVC

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.viewModel = [FFPasswordFreePayVM new];
        self.viewModel.delegate = self;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (NSString *)langTitle {
    return LLang(@"免密支付");
}

#pragma mark - FFPasswordFreePayVMDelegate

- (void)ff_passwordFreePay:(FFPasswordFreePayVM *)vm item:(NSString *)item on:(BOOL)on {
    __weak typeof(self) weakSelf = self;
    if(on) {
        [[FFWalletManager shared] ff_getPayToken:0 item:[item isEqualToString:@"redpacket"]?LIMPayItemFreePWDRedpacket:LIMPayItemFreePWDTransfer remark:LLangW(@"免密", weakSelf) callback:^(NSString * _Nonnull payToken) {
            [weakSelf passwordFreePayUI:item action:@"open" payToken:payToken];
        } cancel:^{
            [weakSelf reloadData];
        }];
    }else{
        [weakSelf passwordFreePayUI:item action:@"close" payToken:nil];
    }
}

-(void) passwordFreePayUI:(NSString*)item action:(NSString*)action payToken:(NSString*)payToken {
    [[WKNavigationManager shared].topViewController.view showHUD];
    __weak typeof(self) weakSelf = self;
    [self.viewModel ff_passwordFreePay:item action:action payToken:payToken].then(^{
        [[WKNavigationManager shared].topViewController.view hideHud];
        
        if([item isEqualToString:@"redpacket"]) {
            [WKApp shared].loginInfo.extra[@"redpacket_passwordfree"] = [action isEqualToString:@"open"]?@(YES):@(NO);
        }else if([item isEqualToString:@"transfer"]) {
            [WKApp shared].loginInfo.extra[@"transfer_passwordfree"] = [action isEqualToString:@"open"]?@(YES):@(NO);
        }
        [[WKApp shared].loginInfo save];
        [weakSelf reloadRemoteData];
    }).catch(^(NSError *error){
        [[WKNavigationManager shared].topViewController.view switchHUDError:error.domain];
    });
}
@end
